package com.adingxiong.cft.quartz;


import lombok.extern.slf4j.Slf4j;
import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @ClassName QuartzManager
 * @Description   定时任务的管理工具类
 * @Author xiongchao
 * @Date 2021/1/11 14:27
 **/
@Slf4j
public class SimpleJobDemo  {

    private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    public static void main(String[] args) throws SchedulerException, InterruptedException {
        JobDetail job = JobBuilder.newJob(SimpleJob.class)
                .withIdentity("simple","group")
                .build();

        Trigger trigger = TriggerBuilder.newTrigger()
                .withIdentity("simple_trigger")
                .startNow()
                .withSchedule(SimpleScheduleBuilder.repeatSecondlyForever(2))
                .build();
        Scheduler scheduler = new StdSchedulerFactory().getScheduler();
        scheduler.scheduleJob(job,trigger);
        scheduler.start();


        Thread.sleep(10_100);
        scheduler.shutdown();
    }

    public static class SimpleJob implements Job{
        @Override
        public void execute(JobExecutionContext context) throws JobExecutionException {
            log.info("======================= simple Job ===========================");
            log.info("now date is :{}" ,sdf.format(new Date()));
            System.out.printf("now date is %s",new Date());
        }
    }

}
